<?php
//exit;
date_default_timezone_set('Asia/Chongqing');
//$dir = dirname(dirname(__FILE__));
//include ($dir."/core/config.inc.php");
if(!empty($_SERVER["QUERY_STRING"])){
	echo "你想干嘛？";
	exit;
}
include 'db.php';//数据库连接
include 'mymc.php';
$cache = new Mymc();
$key = 'api_borrow_list_all';
$list = $cache->get_array($key);

if ($list==false){
	$now = time();
	//$sql = "select * from {borrow} where status=1 or (status=3 and is_circulation=1 and circulation_endtime>UNIX_TIMESTAMP()) ";
	$sql = "select * from ych_borrow where fstatus=1 or (fstatus=3 and ftype=4 and lz_endtime>UNIX_TIMESTAMP() and account!=account_yes) ";
	$list = db_fetch_arrays($sql,$conn);
	$cache->set_array($key, $list, 600);
}
//header("content-Type: text/html; charset=gbk"); 
header('Content-Type: text/xml;');
$dom = new DOMDocument('1.0', 'gbk');
$bidInfos = $dom->createElement('bidInfos');
$dom->appendChild($bidInfos);

foreach($list as $info){
	$txttype='';
	
	if($info["ftype"]==2){
		$txttype = "押";
	}elseif($info["ftype"]==5){
		$txttype = "秒";
	}elseif($info["ftype"]==3){
		$txttype = "净";
	}elseif($info["ftype"]==6){
		$txttype = "担";
	}elseif($info["ftype"]==4){
		$txttype = "流";
	}elseif($info["ftype"]==1){
		$txttype = "信";
	}else{
		$txttype = "荐";
	}
	
	$sql = "select username from ych_user where user_id=".$info["user_id"];
	$row = db_fetch_array($sql,$conn);
	$txtowner = isset($row["username"]) ? $row["username"] : "***";
	
	$bidInfo = $dom->createElement('bidInfo');
	$bidInfos->appendChild($bidInfo);
	
	$id = $dom->createElement('id');
	$idText = $dom->createTextNode($info["id"]);
	$id->appendChild($idText);
	
	$title = $dom->createElement('title');
	$titleName = $info["title"];
	//$titleName = mb_convert_encoding($info["name"],"UTF-8","GBK");
	$titleText = $dom->createTextNode($titleName);
	$title->appendChild($titleText);
	
	$bidInfoUrl = $dom->createElement('bidInfoUrl');
	$tmpstr = "http://www.gsdai.com/borrow/obj/".$info["id"];
	$bidInfoUrlText = $dom->createTextNode($tmpstr);
	$bidInfoUrl->appendChild($bidInfoUrlText);
	
	$bidUrl = $dom->createElement('bidUrl');
	$tmpstr = "http://www.gsdai.com/borrow/obj/".$info["id"];
	$bidUrlText = $dom->createTextNode($tmpstr);
	$bidUrl->appendChild($bidUrlText);
	
	$type = $dom->createElement('type');
	//$txttype = mb_convert_encoding($txttype,"UTF-8","GBK");
	$typeText = $dom->createTextNode($txttype);
	$type->appendChild($typeText);
	
	$money = $dom->createElement('money');
	$moneyText = $dom->createTextNode($info["account"]);
	$money->appendChild($moneyText);
	
	$yearRate = $dom->createElement('yearRate');
	$yearRateText = $dom->createTextNode($info["apr"]);
	$yearRate->appendChild($yearRateText);
	
	$prize = $dom->createElement('prize');
	$prizeText = $dom->createTextNode($info["funds"]);
	$prize->appendChild($prizeText);
	
	$prizeMoney = $dom->createElement('prizeMoney');
	$prizeMoneyText = $dom->createTextNode('0');
	$prizeMoney->appendChild($prizeMoneyText);
	
	$limitTime = $dom->createElement('limitTime');
	$txtlimitTime = $info["is_day"] == 1 ? $info["time_limit_day"]/100 : $info["time_limit"];
	$limitTimeText = $dom->createTextNode($txtlimitTime);
	$limitTime->appendChild($limitTimeText);
	
	$repaymentType = $dom->createElement('repaymentType');
	//if(isset($info["is_circulation"]) && $info["is_circulation"]==1){
	if($info['ftype']==4){	
		$repaymentTypeText = $dom->createTextNode("3");
	}else{
		$repaymentTypeText = $dom->createTextNode("1");
	}
	
	$repaymentType->appendChild($repaymentTypeText);
	
	$process = $dom->createElement('process');
	$txtprocess  = intval($info["account_yes"]/$info["account"]*10000)/100;
	$processText = $dom->createTextNode($txtprocess);
	$process->appendChild($processText);
	
	$owner = $dom->createElement('owner');
	//$txtowner = mb_convert_encoding($txtowner,"UTF-8","GBK");
	$ownerText = $dom->createTextNode($txtowner);
	$owner->appendChild($ownerText);
	
	$loanPlatform = $dom->createElement('loanPlatform');
	$loanPlatformText = $dom->createTextNode("GSD");
	$loanPlatform->appendChild($loanPlatformText);
	
	$fee = $dom->createElement('fee');
	$feeText = $dom->createTextNode("0");
	$fee->appendChild($feeText);
	
	$hasPwd = $dom->createElement('hasPwd');
	$hasPwdText = $dom->createTextNode($info["is_dxb"]);
	$hasPwd->appendChild($hasPwdText);
	
	$dayBid = $dom->createElement('dayBid');
	$dayBidText = $dom->createTextNode($info["is_day"]);
	$dayBid->appendChild($dayBidText);
	
	$startTime = $dom->createElement('startTime');
	$verify_time = date('YmdHis', $info["verify_time"]);
	$startTimeText = $dom->createTextNode($verify_time);
	$startTime->appendChild($startTimeText);
	
	$endTime = $dom->createElement('endTime');
	$endTimeText = $dom->createTextNode("");
	$endTime->appendChild($endTimeText);
	
	$bidInfo->appendChild($id);
	$bidInfo->appendChild($title);
	$bidInfo->appendChild($bidInfoUrl);
	$bidInfo->appendChild($bidUrl);
	$bidInfo->appendChild($type);
	$bidInfo->appendChild($money);
	$bidInfo->appendChild($yearRate);
	$bidInfo->appendChild($prize);
	$bidInfo->appendChild($prizeMoney);
	$bidInfo->appendChild($limitTime);
	$bidInfo->appendChild($repaymentType);
	$bidInfo->appendChild($process);
	$bidInfo->appendChild($owner);
	$bidInfo->appendChild($loanPlatform);
	$bidInfo->appendChild($fee);
	$bidInfo->appendChild($hasPwd);
	$bidInfo->appendChild($dayBid);
	$bidInfo->appendChild($startTime);
	$bidInfo->appendChild($endTime);
	
}
$dom->formatOutput = true;
$xmlString = $dom->saveXML();
echo $xmlString;


function db_fetch_arrays($sql, $conn) 	{
	$result = mysql_query($sql, $conn);
	$i = 0;
	$_res = array();
	while(@$res = mysql_fetch_array($result,MYSQL_ASSOC)) {
		foreach ($res as $key => $value){
			$_res[$i][$key] = $value;//直接转义
		}
		$i++;
	}
	return $_res;
}

function db_fetch_array($sql,$conn) {
		$result = mysql_query($sql, $conn);
		$res = mysql_fetch_array($result,MYSQL_ASSOC);
		$_res = "";
		if (is_array($res)){
			foreach ($res as $key => $value){
				$_res[$key] =$value;
			}
		}
		$query = $_res;
		return $query;
}

?>
